package com.cf.web.excel;




import org.apache.poi.ss.usermodel.Cell;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * cf-game-DefaultFormater
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/4/24 下午 06:18
 */
public class DefaultFormatter implements ExcelDataFormatter {

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    public void outPutFormate(Cell cell, Object data) {
        if (data == null) {
            //处理无数据情况
            cell.setCellValue("");
            return;
        }
        // 处理日期类型
        if (data instanceof Date) {
            cell.setCellValue(handleDate((Date) data));
        } else if (data instanceof Double || data instanceof Float) {
            cell.setCellValue(handleNum(data));
        } else if (data instanceof Boolean) {
            cell.setCellValue(handleBool((Boolean) data));
        } else if (data instanceof Integer) {
            cell.setCellValue(handleInteger((Integer) data));
        } else {
            cell.setCellValue(handleString(data));
        }
    }

    private String handleString(Object data) {
        return data.toString();
    }

    private Integer handleInteger(Integer data) {
        return data;
    }

    private String handleBool(boolean data) {
        return data ? "对" : "错";
    }

    private double handleNum(Object data) {
        return (Double) data;
    }

    private String handleDate(Date time) {
        return simpleDateFormat.format(time);
    }

    @Override
    public void inPutConvert(Object original, String originalKey, Object target, Field targetField) throws Exception {
        if (targetField.getName().equals(originalKey)) {
            if (targetField.getType().equals(Date.class)) {
                if (original.getClass().equals(Date.class)) {
                    targetField.set(target, original);
                } else {
                    targetField.set(target, parseDate(original));
                }
            } else if (targetField.getType().equals(String.class)) {
                targetField.set(target, original);
            } else if (targetField.getType().equals(Long.class)) {
                if (original.getClass().equals(Long.class)) {
                    targetField.set(target, original);
                } else {
                    targetField.set(target, parseLong(original));
                }
            } else if (targetField.getType().equals(Integer.class)) {
                if (original.getClass().equals(Integer.class)) {
                    targetField.set(target, original);
                } else {
                    targetField.set(target, parseInteger(original));
                }
            } else if (targetField.getType().equals(BigDecimal.class)) {
                if (original.getClass().equals(BigDecimal.class)) {
                    targetField.set(target, original);
                } else {
                    targetField.set(target, parseBigDecimal(original));
                }
            } else if (targetField.getType().equals(Boolean.class)) {
                if (original.getClass().equals(Boolean.class)) {
                    targetField.set(target, original);
                } else {
                    targetField.set(target, parseBoolean(original));
                }
            } else if (targetField.getType().equals(Float.class)) {
                targetField.set(target, parseFloat(original));
            } else if (targetField.getType().equals(Double.class)) {
                targetField.set(target, parseDouble(original));
            }

        }
    }

    private double parseDouble(Object original) {
        return Double.parseDouble(original.toString());
    }

    private float parseFloat(Object original) {
        return Float.parseFloat(original.toString());
    }

    private boolean parseBoolean(Object original) {
        return Boolean.parseBoolean(original.toString());
    }

    private BigDecimal parseBigDecimal(Object original) {
        return BigDecimal.valueOf(Double.parseDouble(original.toString()));
    }

    private int parseInteger(Object original) {
        return Integer.parseInt(original.toString());
    }

    private long parseLong(Object original) {
        return Long.parseLong(original.toString());
    }

    private Date parseDate(Object original) throws ParseException {
        return simpleDateFormat.parse(original + "");
    }
}
